package com.example.mongo.Model.dto;



import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import java.io.Serializable;

//前后端分离，返回数据封装统一的格式
//成功对应 code:200 并且将数据传入 Data
//失败对应 code：404 or 400 .. 输出 msg 错误信息

@Component
public class ResponseData implements Serializable {
    // 响应信息
    private String msg;

    private boolean isSUCCESS;
    // 响应code
    private String code;
    // 操作成功，响应数据  Json字符串格式
    private String data;

    public ResponseData(String msg, boolean isSUCCESS, String code, String data) {
        this.msg = msg;
        this.isSUCCESS = isSUCCESS;
        this.code = code;
        this.data = data;
    }

    public ResponseData() {
    }

    @Override
    public String toString() {
        JSONObject result = new JSONObject();
        result.put("code", code);
        result.put("message", msg);
        result.put("isSUCCESS", isSUCCESS);
        result.put("data", data);
        System.out.println(result.toString());
        return result.toString();
    }


    public void success(String msg,String data) {
        //this (Msg, true,"200", data);
        this.msg = msg;
        this.code = "200";
        this.isSUCCESS = true;
        this.data = data;
    }

    public void error(String code, String msg,String data) {
        this.msg = msg;
        this.code = code;
        this.isSUCCESS = false;
        this.data = data;
    }


}
